package com.et114.modules.myet.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.et114.core.action.BaseDispatchAction;
import com.et114.core.exception.CheckException;
import com.et114.modules.products.commons.Const;
import com.et114.modules.products.commons.SessionUser;

public class MyEtAction extends BaseDispatchAction { 
	protected String	LAST_URL = "LAST_URL";
	
	protected Integer getCustId ( HttpServletRequest request )
			throws CheckException {
		HttpSession session = request.getSession ( );
		Object o = session.getAttribute ( Const.SESSION_USER_KEY );
		if ( o == null )
			return null;
		SessionUser sessionUser = ( SessionUser ) o;
		return sessionUser.getCustId ( );
	}
	
	protected String getUserId ( HttpServletRequest request )
			throws CheckException {
		HttpSession session = request.getSession ( );
		Object o = session.getAttribute ( Const.SESSION_USER_KEY );
		if ( o == null )
			return null;
		SessionUser sessionUser = ( SessionUser ) o;
		return sessionUser.getUserId ( ).toString ( );
	}
	
	protected boolean isNeedLogin (HttpServletRequest request ) {
		String last_url=request.getRequestURL ( ) +"?"+request.getQueryString ( );
		return isNeedLogin(request,last_url);
	}
	protected boolean isNeedLogin (HttpServletRequest request ,String toUrl) {
		if ( getCustId ( request ) == null ) {
			HttpSession session = request.getSession ( );
			session.setAttribute ( LAST_URL ,toUrl); 
			request.setAttribute ( "msg" , "请登陆后再操作!" );
			request.setAttribute ( "back" , "login.html" ); 
			return true;
		}
		return false;
	}
	
}
